#pragma once

#include "Object/Object.h"
#include "RtAudioWrapper.h"
#include "Containers/ScriptDelegateImplement.h"
#include "AudioPlayer.m.h"

class RAudio;



RCLASS()
class WHENGINE_API RAudioPlayer : public RObject
{
    ROBJECT
public:

    TSharedObjectPtr<RAudio> GetAudio() const;
    void SetAudio(TSharedObjectPtr<RAudio> InAudio);

    float Volume=1.0f; //音量 0-1
    

    int64_t GetFrameDataSize();
    
    void Open();
    void Close();
    bool IsOpen();

    void Play();
    void Pause();
    void Abort(); //stop and reset to start
    bool IsPlaying();

    RFIELD()
    TScriptDelegate<void()> OnPlayFinished;


    //return the current time of the audio in seconds
    void SetCurrentTime(float InTime);
    float GetCurrentTime();

    float GetCurrentTimePercent();
    void SetCurrentTimePercent(float InPercent);

protected:
    RtAudioWarpper Player;
    TSharedObjectPtr<RAudio> Audio;
    
    /// @brief 
    /// @param outputBuffer 
    /// @param nBufferFrames 
    /// @return how many frames are copied,if left frames are not enough will < nBufferFrames
    uint64_t CopyData(void *outputBuffer, uint64_t nBufferFrames);

    uint64_t FrameCount=0;
    uint64_t PlayedFrameNum=0;

};